package com.talent.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.talent.common.PageResult;
import com.talent.mapper.NoticeMapper;
import com.talent.pojo.dto.NoticeDTO;
import com.talent.pojo.entity.Notice;
import com.talent.service.NoticeService;
import com.talent.service.OperationLogService;
import com.talent.utils.OperationLogUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 通知服务实现类
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private OperationLogService operationLogService;

    /**
     * 分页查询通知
     */
    @Override
    public PageResult<Notice> pageQuery(Integer page, Integer pageSize, String title) {
        PageHelper.startPage(page, pageSize);
        Page<Notice> p = (Page<Notice>) noticeMapper.pageQuery(title);
        return new PageResult<>(p.getResult(), p.getTotal(), page, pageSize);
    }

    /**
     * 根据ID查询通知
     */
    @Override
    public Notice getById(Long id) {
        return noticeMapper.getById(id);
    }

    /**
     * 新增通知
     */
    @Override
    public void save(NoticeDTO noticeDTO) {
        // 验证通知类型及对应字段
        validateNoticeDTO(noticeDTO);
        
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO, notice);
        noticeMapper.insert(notice);

        // 记录详细操作日志
        operationLogService.saveDetail(OperationLogUtils.buildCreateLog(
                "notice", notice.getId(), notice.getTitle(), notice));
    }

    /**
     * 更新通知
     */
    @Override
    public void update(Long id, NoticeDTO noticeDTO) {
        // 验证通知类型及对应字段
        validateNoticeDTO(noticeDTO);
        
        // 获取更新前的数据
        Notice oldNotice = noticeMapper.getById(id);
        
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO, notice);
        notice.setId(id);
        noticeMapper.update(notice);

        // 获取更新后的数据
        Notice newNotice = noticeMapper.getById(id);
        
        // 记录详细操作日志
        operationLogService.saveDetail(OperationLogUtils.buildUpdateLog(
                "notice", id, newNotice.getTitle(), oldNotice, newNotice));
    }
    
    /**
     * 验证通知DTO的字段
     */
    private void validateNoticeDTO(NoticeDTO noticeDTO) {
        String type = noticeDTO.getType();
        
        // 验证type字段的有效性
        if (!"link".equals(type) && !"manual".equals(type)) {
            throw new IllegalArgumentException("通知类型只能是 link 或 manual");
        }
        
        // 根据类型验证对应的必填字段
        if ("link".equals(type)) {
            if (noticeDTO.getLinkUrl() == null || noticeDTO.getLinkUrl().trim().isEmpty()) {
                throw new IllegalArgumentException("当通知类型为链接地址时,链接地址不能为空");
            }
        } else if ("manual".equals(type)) {
            if (noticeDTO.getContent() == null || noticeDTO.getContent().trim().isEmpty()) {
                throw new IllegalArgumentException("当通知类型为手动编辑时,通知内容不能为空");
            }
        }
    }

    /**
     * 删除通知
     */
    @Override
    public void deleteById(Long id) {
        // 获取删除前的数据
        Notice notice = noticeMapper.getById(id);
        
        noticeMapper.deleteById(id);

        // 记录详细操作日志
        operationLogService.saveDetail(OperationLogUtils.buildDeleteLog(
                "notice", id, notice.getTitle(), notice));
    }

    /**
     * 批量删除通知
     */
    @Override
    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}